package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.base.RecycleViewDivider;
import com.wnhz.work.yz.bean.YanShouDaiBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class YanShouDaiSearchActivity extends BaseActivity implements View.OnClickListener {
    private EditText et_search;
    private RecyclerView search_recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_dai_search);
        et_search = (EditText) findViewById(R.id.et_search);
        search_recycler = (RecyclerView) findViewById(R.id.recycler);
        search_recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        search_recycler.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 10, getResources().getColor(R.color.main_background2)));
        findViewById(R.id.cancel).setOnClickListener(this);
        et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    upSearch();
                    return true;
                }
                return false;
            }
        });
    }

    private List<YanShouDaiBean.InfoEntity> info = new ArrayList<>();

    private void upSearch() {
        if ("".equals(et_search.getText().toString().trim())) {
            MyToast("请输入内容");
            return;
        }
        Map<String, Object> map = new Hashtable<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("keywords", et_search.getText().toString());
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, "" + value + "\n");
            }
        }
        String url="";
        if (getIntent().getIntExtra("jicha",0)==1){
            url=Url.DECORATE_AUDITINGLIST;
        }else{
            url=Url.DECORATE_WAITIMGCHECKLIST;

        }

        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info3 = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouDaiBean bean = gson.fromJson(result, YanShouDaiBean.class);
                        info = bean.getInfo();
                        setData();
                        search_recycler.setVisibility(View.VISIBLE);
                    } else {
                        search_recycler.setVisibility(View.GONE);
                        if ("-1".equals(res)) {
                            info3 = "请重新登录";
                        }
                        MyToast(info3);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
        }
    }

    private void setData() {
        search_recycler.setAdapter(new BaseRVAdapter(this, info) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_dai;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                if (info.get(position).getImg() != null && info.get(position).getImg().size() > 0) {
                    x.image().bind(holder.getImageView(R.id.img2), info.get(position).getImg().get(0));
                    holder.getTextView(R.id.name).setText(info.get(position).getImg().get(0));
                }
                holder.getTextView(R.id.name).setText(info.get(position).getProject_name());
                holder.getTextView(R.id.hetonghao).setText("合同编号：" + info.get(position).getBudget_no());
                holder.getTextView(R.id.time).setText("" + info.get(position).getCheck_time());
                holder.getTextView(R.id.address).setText("" + info.get(position).getAddress());
                holder.getTextView(R.id.anzhuang).setText("安装队：" + info.get(position).getInstall());
                holder.getTextView(R.id.or).setText("总套数：" + info.get(position).getAll_nums() + "套/已报检套数：" + info.get(position).getHas_nums() + "套");

            }

        });
    }
}
